/*
 * 文件名：HttpClient.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：wootide-0005
 * 修改时间：2013-3-6
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpClientToCard
{
    String urlAddress = "http://127.0.0.1:8080/test.jsp";
    
    URL url;
    
    HttpURLConnection uRLConnection;
    
    public HttpClientToCard()
    {
    }
    
    //向服务器发送get请求  
    public String doGet(String username, String password)
    {
        String getUrl = urlAddress + "?name=" + username + "&password="
                + password;
        try
        {
            url = new URL(getUrl);
            uRLConnection = (HttpURLConnection)url.openConnection();
            InputStream is = uRLConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String response = "";
            String readLine = null;
            while ((readLine = br.readLine()) != null)
            {
                //response = br.readLine();                  
                response = response + readLine;
            }
            is.close();
            br.close();
            uRLConnection.disconnect();
            return response;
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
            return null;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            
            return null;
        }
    } 
    //向服务器发送post请求    
    public String doPost(String username, String password)
    {
        try
        {
            url = new URL(urlAddress);
            uRLConnection = (HttpURLConnection)url.openConnection();
            uRLConnection.setDoInput(true);
            uRLConnection.setDoOutput(true);
            uRLConnection.setRequestMethod("POST");
            uRLConnection.setUseCaches(false);
            uRLConnection.setInstanceFollowRedirects(false);
            uRLConnection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            uRLConnection.connect();
            DataOutputStream out = new DataOutputStream(
                    uRLConnection.getOutputStream());
            String content = "username=" + username + "&password=" + password;
            out.writeBytes(content);
            out.flush();
            out.close();
            InputStream is = uRLConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String response = "";
            String readLine = null;
            while ((readLine = br.readLine()) != null)
            {
                response = response + readLine;
            }
            is.close();
            br.close();
            uRLConnection.disconnect();
            return response;
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
            return null;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    public static void main(String[] args)
    {
        new HttpClientToCard().doGet("good","ss");
    }
}
